package com.javacto.mapper;

import com.javacto.pojo.TUser;
import com.javacto.pojo.TUserExample;
import com.javacto.pojo.TUserExample.Criteria;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Date;
import java.util.List;

/**
 * describe  单元测试  Dao
 * 作者: 英杰
 */
@RunWith(SpringJUnit4ClassRunner.class) //表示继承了SpringJUnit4ClassRunner类
//告诉junit spring配置文件
@ContextConfiguration( { "classpath:beans.xml" })
public class UserDaoTest {
    @Autowired
    private TUserMapper tUserMapper;


    /**
     * 测试查询所有数据
     * public List<TUser> selectByExample(TUserExample example);
     */
    @Test
    public void test01(){
        TUserExample example=new TUserExample();
        List <TUser> list =tUserMapper.selectByExample(example);
        for (TUser u : list) {
            System.out.println(u);
        }
    }

    /**
     * 查询 添加条件
     * 需要传 Criteria
     * protected List<Criteria> oredCriteria;
     */
    @Test
    public void test02(){
        //1.创建 TUserExample
        TUserExample example=new TUserExample();
        //创建Criteria
        Criteria criteria=example.createCriteria();
        //传入参数
        //criteria.andUnameEqualTo("李四");  //查询姓名为李四的 所有数据
        criteria.andSexEqualTo("1");  //查询性别为1的 所有数据
        List <TUser> list =tUserMapper.selectByExample(example);
        for (TUser u : list) {
            System.out.println(u);
        }
    }

    /**
     * 测试03  删除
     * int deleteByPrimaryKey(Integer tid);
     * int deleteByExample(TUserExample example);
     */
    @Test
    public void test03(){
        //1.创建 TUserExample
        TUserExample example=new TUserExample();
        //创建Criteria
        Criteria criteria=example.createCriteria();

        /*int num=tUserMapper.deleteByPrimaryKey(1003);
        if (num>0){
            System.out.println("删除成功");
        }*/
        //或者使用 第二种
        criteria.andTidEqualTo(1004);
        tUserMapper.deleteByExample(example);
    }

    /**
     * 测试04 添加
     */
    @Test
    public void test04(){
        TUser tUser=new TUser();

        tUser.setTid(1003);
        tUser.setAddress("北京");
        tUser.setBirthday(new Date());
        tUser.setSex("2");
        tUser.setUname("小黑");
        int num=tUserMapper.insert(tUser);
        if (num>0){
            System.out.println("添加成功");
        }
    }


    /**
     * 测试05 修改
     * int updateByPrimaryKeySelective(TUser record);
     * 此方法 可选择修改
     */
    @Test
    public void test05(){

        TUser tUser=new TUser();

        tUser.setTid(1003);
       /* tUser.setAddress("北京1");
        tUser.setBirthday(new Date());
        tUser.setSex("1");*/
        tUser.setUname("小黑122");
        int num = tUserMapper.updateByPrimaryKeySelective(tUser);
        if (num>0){
            System.out.println("添加成功");
        }
    }

    /**
     * 测试06  查看总共有几条数据
     */
    @Test
    public void test06(){
        TUserExample example=new TUserExample();

        long l = tUserMapper.countByExample(example);
        System.out.println(l);
    }
}
